package com.work.saturn.interesting;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.immo.libcommon.base.BaseHeadActivity;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.constant.RouterUtil;
import com.immo.libcommon.constant.SharePreferenceKey;
import com.immo.libcommon.http.NetworkRequest;
import com.immo.libcommon.utils.FullyLinearLayoutManager;
import com.immo.libcommon.utils.SharePreferenceUtils;
import com.work.saturn.R;
import com.work.saturn.assets.PayDetailFragment;
import com.work.saturn.bean.GASBean;
import com.work.saturn.bean.InterestDetailBean;
import com.work.saturn.bean.InterestSubmitBean;
import com.work.saturn.bean.MessageEvent;
import com.work.saturn.bean.MyDetailBean;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author YaoChen
 * @content 未来局详情
 * @date 2018/07/16
 */
@Route(path = RouterUtil.INTER_DETAIL)
public class InterestDetailActivity extends BaseHeadActivity {
    @BindView(R.id.interest_detail_title)
    TextView interestDetailTitle;
    @BindView(R.id.interest_detail_person)
    TextView interestDetailPerson;
    @BindView(R.id.interest_detail_time)
    TextView interestDetailTime;
    @BindView(R.id.interest_detail_list)
    RecyclerView mList;
    @BindView(R.id.interest_detail_password)
    EditText interestDetailPassword;
    @BindView(R.id.interest_detail_ll)
    LinearLayout interestDetailLl;
    @BindView(R.id.interest_detail_back)
    TextView interestDetailBack;
    @BindView(R.id.interest_detail_submit)
    TextView interestDetailDW;
    @BindView(R.id.interest_detail_crea)
    TextView interestDetailCrea;
    @BindView(R.id.interest_detail_total)
    TextView interestDetailTotal;
    @BindView(R.id.interest_detail_result)
    TextView interestDetailResult;
    private List<String> datas = new ArrayList<>();
    private List<Integer> datass = new ArrayList<>();

    private String interName = "worldCup";
    private int selectId = -1;
    private InterestDetailBean interestDetailBean;
    private MyDetailBean myDetailBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interset_detail);
        //禁止进入时弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        ButterKnife.bind(this);
        setTitle("未来局");
        interName = getIntent().getStringExtra("name");
//        setTitleRight("下注统计", 0);
//        type=getIntent().getIntExtra("type",0);
//        if (type==0){
        
//            interestDetailNet();
//        }else {
            interestMyDetailNet();
//        }

    }

//    @Override
//    protected void onRightClick() {
//        super.onRightClick();
//        ARouter.getInstance().build(RouterUtil.INTER_HISTORY)
//                .withSerializable("interesthistory", (Serializable) interestDetailBean.getPayload().getPayload().getHistory())
//                .navigation();
//    }


    /**
     * 未来局详情接口请求
     */
    @SuppressLint("SetTextI18n")
    private void interestMyDetailNet() {
        HashMap<String, String> params = new HashMap<>();
        params.put("futureBureauName", interName);
        params.put("username", SharePreferenceUtils.get(InterestDetailActivity.this, SharePreferenceKey.USERNAME,"").toString());
        new NetworkRequest((object, data) -> {
            if (object instanceof MyDetailBean) {
                myDetailBean = (MyDetailBean) object;
                if (myDetailBean.isSuccess()) {
                    interestDetailTitle.setText(myDetailBean.getPayload().getContent());
                    interestDetailCrea.setText("创建者：" + myDetailBean.getPayload().getCreator());
                    interestDetailPerson.setText("公证人：" + myDetailBean.getPayload().getJudgePerson());
                    interestDetailResult.setText("判断结果：" + myDetailBean.getPayload().getResult());
                    interestDetailTime.setText("截止时间：" + myDetailBean.getPayload().getEndTime());
                    interestDetailTotal.setVisibility(View.GONE);
                    if (myDetailBean.getPayload().getOptions().size()==5){
                        datas.add(myDetailBean.getPayload().getOptions().get(0).getOption());
                        datas.add(myDetailBean.getPayload().getOptions().get(1).getOption());
                        datas.add(myDetailBean.getPayload().getOptions().get(2).getOption());
                        datas.add(myDetailBean.getPayload().getOptions().get(3).getOption());
                        datas.add(myDetailBean.getPayload().getOptions().get(4).getOption());
                        datass.add(myDetailBean.getPayload().getOptions().get(0).getOptionSum());
                        datass.add(myDetailBean.getPayload().getOptions().get(1).getOptionSum());
                        datass.add(myDetailBean.getPayload().getOptions().get(2).getOptionSum());
                        datass.add(myDetailBean.getPayload().getOptions().get(3).getOptionSum());
                        datass.add(myDetailBean.getPayload().getOptions().get(4).getOptionSum());
                    }
                    initData();
                }
            }
        }).requestPost(BaseUrl.GET_XIZZHUDE, this, JSON.toJSONString(params), MyDetailBean.class, true, false);
    }
    /**
     * 未来局详情接口请求
     */
    @SuppressLint("SetTextI18n")
//    private void interestDetailNet() {
//        HashMap<String, String> params = new HashMap<>();
//        params.put("futureBureauName", interName);
//        new NetworkRequest((object, data) -> {
//            if (object instanceof InterestDetailBean) {
//                interestDetailBean = (InterestDetailBean) object;
//                if (interestDetailBean.isSuccess()) {
//                    interestDetailTitle.setText(interestDetailBean.getPayload().getPayload().getContent());
//                    interestDetailCrea.setText("创建者：" + interestDetailBean.getPayload().getPayload().getCreator());
//                    interestDetailPerson.setText("公证人：" + interestDetailBean.getPayload().getPayload().getJudgePerson());
//                    interestDetailResult.setText("判断结果：" + interestDetailBean.getPayload().getPayload().getResult());
//                    interestDetailTime.setText("截止时间：" + interestDetailBean.getPayload().getPayload().getEndTime());
//                    interestDetailTotal.setText("共计已下注： " + interestDetailBean.getPayload().getPayload().getCount() + "GZH");
//                    interestDetailDW.setText("GZH");
//                    datas.add(interestDetailBean.getPayload().getPayload().getOption1());
//                    datas.add(interestDetailBean.getPayload().getPayload().getOption2());
//                    datas.add(interestDetailBean.getPayload().getPayload().getOption3());
//                    datas.add(interestDetailBean.getPayload().getPayload().getOption4());
//                    datas.add(interestDetailBean.getPayload().getPayload().getOption5());
//                    datass.add(interestDetailBean.getPayload().getPayload().getCount1());
//                    datass.add(interestDetailBean.getPayload().getPayload().getCount2());
//                    datass.add(interestDetailBean.getPayload().getPayload().getCount3());
//                    datass.add(interestDetailBean.getPayload().getPayload().getCount4());
//                    datass.add(interestDetailBean.getPayload().getPayload().getCount5());
//                    initData();
//                }
//            }
//        }).requestPost(BaseUrl.INTERESTDETAIL, this, JSON.toJSONString(params), InterestDetailBean.class, true, false);
//    }



    private void initData() {
        FullyLinearLayoutManager linearLayoutManagerTheme = new FullyLinearLayoutManager(this);
        linearLayoutManagerTheme.setOrientation(LinearLayoutManager.VERTICAL);
        mList.setLayoutManager(linearLayoutManagerTheme);
        InterestingListAdapter releaseDetailAdapter = new InterestingListAdapter();
        //设置适配器
        releaseDetailAdapter.bindToRecyclerView(mList);
    }

    /**
     * 资产历史列表
     */
    private class InterestingListAdapter extends BaseQuickAdapter<String, BaseViewHolder> implements BaseQuickAdapter.OnItemClickListener {
        InterestingListAdapter() {
            super(R.layout.item_interes_detail, datas);
            setOnItemClickListener(this);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
//            if (type==0){
//                if (interestDetailBean.getPayload().getPayload().getCount()!=0){
//                    // 创建一个数值格式化对象
//                    NumberFormat numberFormat = NumberFormat.getInstance();
//                    // 设置精确到小数点后2位
//                    numberFormat.setMaximumFractionDigits(2);
//                    String result = numberFormat.format((float)datass.get(helper.getLayoutPosition())/(float)interestDetailBean.getPayload().getPayload().getCount()*100);
//                    helper.setText(R.id.interest_detail_bai,result+"%("+datass.get(helper.getLayoutPosition()).toString()+"/"+interestDetailBean.getPayload().getPayload().getCount()+")");
//                }
//                helper.setText(R.id.interest_detail_total,datass.get(helper.getLayoutPosition())+"GZH");
//            }else {
                helper.setText(R.id.interest_detail_total,myDetailBean.getPayload().getOptions().get(helper.getLayoutPosition()).getOptionSum()+"GZH");
                helper.setText(R.id.interest_detail_bai,((int)(myDetailBean.getPayload().getOptions().get(helper.getLayoutPosition()).getPercentage()))+"%("+myDetailBean.getPayload().getOptions().get(helper.getLayoutPosition()).getSingleCount()+"/"+myDetailBean.getPayload().getOptions().get(helper.getLayoutPosition()).getCount()+")");
//            }
            helper.setText(R.id.interest_detail_person, item);
            if (helper.getLayoutPosition() == selectId) {
                helper.setImageResource(R.id.interest_detail_view, R.drawable.bg_ring_02);
            } else {
                helper.setImageResource(R.id.interest_detail_view, R.drawable.bg_ring);
            }
        }

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            selectId = position;
            notifyDataSetChanged();
        }
    }

    /**
     * 输入的本地判断
     *
     * @return true 通过 false 不通过
     */
    private boolean isCheck() {
        if (selectId == -1) {
            toast("至少选择一项");
            return false;
        }
        if (interestDetailPassword.getText().length() == 0) {
            toast("金额不能为空");
            return false;
        }
        if (Double.parseDouble(interestDetailPassword.getText().toString()) <= 0) {
            toast("下注金额不能为0");
            return false;
        }
        return true;
    }

    @OnClick({R.id.interest_detail_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.interest_detail_back:
                if (isCheck()) {

                    HashMap<String, String> params = new HashMap<>();
                    params.put("amount", interestDetailPassword.getText().toString());
                    params.put("symbol","GZH");
                    new NetworkRequest((Object object, String String) -> {
                        if (object instanceof GASBean) {
                            GASBean baseBean = (GASBean) object;
                            if (baseBean.isSuccess()) {
                                String futureName;
                                String chooseOptions;
//                                if (type==0){
//                                    futureName=interestDetailBean.getPayload().getPayload().getName();
//                                }else {
                                    futureName=myDetailBean.getPayload().getName();
//                                }
                                chooseOptions="option"+(selectId+1);
                                PayDetailFragment payDetailFragment = new PayDetailFragment("GZH",SharePreferenceUtils.get(InterestDetailActivity.this,SharePreferenceKey.ID,"").toString(),interestDetailPassword.getText().toString(),"5813dc05-c645-4cb2-ad73-159a9375517b",""+baseBean.getPayload().getPayload().getGas(),2,futureName,chooseOptions);
                                payDetailFragment.show(getSupportFragmentManager(), "payDetailFragment");
                            }

                        }
                    }).requestPost(BaseUrl.GET_GAS, InterestDetailActivity.this, JSON.toJSONString(params), GASBean.class, true, false);                }
                break;
        }
    }
}
